package com.platform.web.lisenter;

import com.alibaba.fastjson.JSONObject;
import com.platform.base.domain.TempInfo;
import com.platform.base.service.ITempInfoService;
import com.platform.common.manager.AsyncManager;
import com.platform.common.utils.netty.NettyUtil;
import com.platform.common.utils.redis.RedisUtil;
import com.platform.common.utils.spring.SpringUtils;
import com.platform.data.domain.TempData;
import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import com.platform.data.service.ITempDataService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.Date;
import java.util.TimerTask;

@Component
@Slf4j
public class TempListener {

    @Autowired
    private RedisUtil redisUtil;

    @RabbitListener(queues = "temp_queue")
    @RabbitHandler
    public void receiveTemp(JSONObject object, Channel channel, Message message){
        try {
            channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
            String messageId = message.getMessageProperties().getHeaders().get("spring_returned_message_correlation").toString();
            if(redisUtil.hasKey("message:"+messageId)){
                return;
            }
            redisUtil.set("message:"+messageId, messageId, 1000);
            log.info("异步保存温度采集器数据"+object.getString("datas"));
            AsyncManager.me().execute(TempPoolTask.saveTemp(object.getString("datas"), object.getDate("createTime")));
        }catch (IOException e){
            e.printStackTrace();
        }
    }
}

class TempPoolTask {
    public static TimerTask saveTemp(final String datas, final Date createTime) {
        ITempDataService tempDataService = SpringUtils.getBean(ITempDataService.class);
        ITempInfoService tempInfoService =SpringUtils.getBean(ITempInfoService.class);

        return new TimerTask()
        {
            @Override
            public void run()
            {
                //将消息内容拆分称数组
                String[] contents = datas.split(" ");
                //设备id
                String tempId = contents[23]+contents[24]+contents[25]+contents[26]+contents[27]+contents[28]+contents[29]+
                        contents[30]+contents[31]+contents[32]+contents[33]+contents[34]+contents[35]+contents[36]+contents[37];
                tempId = NettyUtil.hexStr2Str(tempId);
                TempInfo info = tempInfoService.selectTempInfoById(tempId);
                try {
                    //室内温度
                    String temp = NettyUtil.get10HexNum(contents[64]) + "." + NettyUtil.get10HexNum(contents[65]);
                    //电池电量
                    String elec = NettyUtil.get10HexNum(contents[67]) + "." + NettyUtil.get10HexNum(contents[68]);
                    //内部NTC故障报警
                    String alarm1 = contents[72];
                    //外部NTC故障报警
                    String alarm2 = contents[73];
                    //温湿度传感器故障报警
                    String alarm3 = contents[74];

                    if(!alarm1.equals("00") && !alarm2.equals("00") && !alarm3.equals("00")){
                        String alarmMsg = "";
                        if(alarm1.equals("01")){
                            alarmMsg = "内部NTC故障报警";
                        }else if(alarm2.equals("01")){
                            alarmMsg = "外部NTC故障报警";
                        }else if(alarm3.equals("01")){
                            alarmMsg = "温湿度传感器故障报警";
                        }
                        AsyncManager.me().execute(AlarmPoolTask.saveAlarm(tempId,String.valueOf(info.getLocationId()), "4", "2", "室温采集", alarmMsg));
                    }

                    if(Double.parseDouble(temp)==0){
                        AsyncManager.me().execute(AlarmPoolTask.saveAlarm(tempId,String.valueOf(info.getLocationId()), "4", "3", "室温采集", "温度采集不正确"));
                    }else{
                        TempData tempData = new TempData();
                        tempData.setTempId(tempId);
                        tempData.setTemp(temp);
                        tempData.setElec(elec);
                        tempData.setCreateTime(createTime);
                        tempData.setAlarm1(alarm1);
                        tempData.setAlarm2(alarm2);
                        tempData.setAlarm3(alarm3);
                        tempDataService.insertTempData(tempData);
                    }
                }catch (Exception e){
                    e.printStackTrace();

                    AsyncManager.me().execute(AlarmPoolTask.saveAlarm(tempId,String.valueOf(info.getLocationId()), "4", "3", "室温采集", "室温采集器"+tempId+"数据["+datas+"]有误，解析错误"+e.getMessage()));
                }
            }
        };
    }
}
